package springboot.kafka.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springboot.kafka.util.RedisTemplateUtil;

/**
 * @author chenjiawen
 * @version 1.0
 * @date 2022/6/11 10:53
 */
@RestController
public class RedisTest {

    @Autowired
    RedisTemplateUtil redisTemplateUtil;

    private volatile int cishu = 0;

    @GetMapping("/redisTest")
    public Long redisTest(int num, String key, int n) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        boolean flag = true;
        while (flag) {
            if (cishu == 0) {
                for (int j = 0; j < n; j++) {
                    new Thread(() -> {
                        String value;
                        for (int i = 0; i < num; i++) {
                            value = (String) redisTemplateUtil.get(key);
                            value = value + "1";
                            cishu++;
                        }
                    }).start();
                }
            }
            if (cishu >= 100) {
                flag = false;
            } else {
                try {
                    Thread.sleep(100L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        stopWatch.stop();
        System.out.println(stopWatch.getTotalTimeMillis() - 100L);
        return stopWatch.getTotalTimeMillis();
    }
}
